﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace YJKFamilyLibDataManager
{
	[DefaultProperty("Parent")]
	public abstract class Parent
	{
		[Browsable(false)]
		public Guid Guid
		{
			get
			{
				return this.mGuid;
			}
		}

		[Browsable(false)]
		public string Name
		{
			get
			{
				return this.mName;
			}
			set
			{
				this.mName = value;
			}
		}

		[DisplayName("受限")]
		[Description("是否受限")]
		[Category("属性")]
		public bool Limit
		{
			get
			{
				return this.mLimit;
			}
			set
			{
				this.mLimit = value;
			}
		}

		[Category("属性")]
		[Description("版本")]
		[DisplayName("版本")]
		public string Version
		{
			get
			{
				return this.mVersion;
			}
			set
			{
				this.mVersion = value;
			}
		}

		[DisplayName("描述")]
		[Description("描述")]
		[Category("属性")]
		public string Description
		{
			get
			{
				return this.mDescription;
			}
			set
			{
				this.mDescription = value;
			}
		}

		[Browsable(false)]
		public FamilyLibDatabase Database
		{
			get
			{
				return this.mDatabase;
			}
		}

		public void SetDatabase(FamilyLibDatabase db)
		{
			this.mDatabase = db;
		}

		public List<DbCategory> GetAllChildren(bool isAll)
		{
			List<DbCategory> list = new List<DbCategory>();
			List<DbCategory> list2 = this.mDatabase.CategoryTable[this];
			list.AddRange(list2);
			if (isAll)
			{
				foreach (DbCategory category in list2)
				{
					list.AddRange(this.GetSubCategories(category, isAll));
				}
			}
			return list;
		}

		private List<DbCategory> GetSubCategories(DbCategory category, bool isAll)
		{
			List<DbCategory> list = new List<DbCategory>();
			ICategoryTable categoryTable = this.mDatabase.CategoryTable;
			List<DbCategory> list2 = categoryTable[category];
			list.AddRange(list2);
			if (isAll)
			{
				foreach (DbCategory category2 in list2)
				{
					list.AddRange(this.GetSubCategories(category2, isAll));
				}
			}
			return list;
		}

		public List<DbFamily> GetAllFamilies(bool isAll, CheckType checkType)
		{
			List<DbFamily> list = new List<DbFamily>();
			try
			{
				IFamilyTable familyTable = this.mDatabase.FamilyTable;
				list.AddRange(familyTable[this, checkType]);
				if (isAll)
				{
					List<DbCategory> allChildren = this.GetAllChildren(isAll);
					foreach (DbCategory parent in allChildren)
					{
						list.AddRange(familyTable[parent, checkType]);
					}
				}
			}
			catch
			{
			}
			return list;
		}

		public int GetFamilyCount(bool isAll, CheckType checkType)
		{
			int num = 0;
			IFamilyTable familyTable = this.mDatabase.FamilyTable;
			num = familyTable.Count(this, checkType);
			if (isAll)
			{
				List<DbCategory> allChildren = this.GetAllChildren(isAll);
				foreach (DbCategory parent in allChildren)
				{
					num += familyTable.Count(parent, checkType);
				}
			}
			return num;
		}

		public List<DbFamily> GetFamilies(int start, int end)
		{
			List<DbFamily> list = new List<DbFamily>();
			IFamilyTable familyTable = this.mDatabase.FamilyTable;
			list.AddRange(familyTable[this, start, end, CheckType.All]);
			return list;
		}

		public bool AddCategory(DbCategory category)
		{
			bool result;
			try
			{
				result = this.Database.CategoryTable.Add(this, category);
			}
			catch
			{
				throw new Exception("添加失败，请检查该分类下是否包含重名分类");
			}
			return result;
		}

		public override string ToString()
		{
			return this.mName;
		}

		public override bool Equals(object obj)
		{
			Parent parent = obj as Parent;
			return parent != null && this.mGuid == parent.Guid;
		}

		public override int GetHashCode()
		{
			return this.mName.GetHashCode();
		}

		protected Guid mGuid;

		protected string mName;

		protected bool mLimit;

		protected string mVersion;

		private string mDescription = "";

		protected FamilyLibDatabase mDatabase;
	}
}
